package com.erhya.export.poi.chain.jxl;

import com.erhya.export.poi.JxlExcelTemplate;
import com.erhya.export.poi.chain.ExcelChina;
import jxl.Workbook;
import jxl.write.WritableWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class JxlNewSheetExcelExecute<T> extends ExcelChina<T> {

    private JxlExcelTemplate<T> excelTemplate;

    public JxlNewSheetExcelExecute(JxlExcelTemplate<T> excelTemplate) {
        this.excelTemplate = excelTemplate;
    }

    @Override
    public void execute(String fileName) throws Exception{
        WritableWorkbook workBook = excelTemplate.getWorkbook();
        if (workBook == null && super.workBook == null){
            HttpServletResponse response = excelTemplate.getResponse();
            ServletOutputStream ops = response.getOutputStream();
            workBook = Workbook.createWorkbook(ops);
            super.workBook = workBook;

        }

        excelTemplate.setWorkbook((WritableWorkbook) super.workBook);

        if (successor != null ){

            excelTemplate.china(fileName, true);

            successor.setWorkBook(super.workBook);
            successor.execute(fileName);
            return;
        }

        excelTemplate.china(fileName, false);

    }

}
